package com.tangxz.generate_code.utils;

//import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang.time.DateFormatUtils;

import java.lang.reflect.Field;
import java.util.Date;

/**
 * @author: Tangxz
 * @email: 1171702529@qq.com
 * @cate: 2021/12/15 09:49
 */
public class ReflexUtil {

    public static String getFieldValueByFieldName(String fieldName, Object object) {
        try {
            Field field = object.getClass().getDeclaredField(fieldName);
            //设置对象的访问权限，保证对private的属性的访问
            field.setAccessible(true);
            Object hisValue = field.get(object);
            if (null == hisValue) {
                return "";
            }

            String value = "";
            String type = field.getType().toString();
            if (type.contains("Date")) {
                value = DateFormatUtils.format((Date) hisValue, "yyyy-MM-dd HH:mm:ss");
            } else {
                value = hisValue.toString();
            }

            return value;
        } catch (Exception e) {

            return "";
        }
    }

    public static String getFieldTypeByFieldName(String fieldName, Object object) {
        try {
            Field field = object.getClass().getDeclaredField(fieldName);
            //设置对象的访问权限，保证对private的属性的访问
            field.setAccessible(true);
            String type = field.getType().toString();
            return type;
        } catch (Exception e) {

            return "";
        }
    }
//
//    public static String getFieldAnnotation(String fieldName, Object object) {
//        try {
//            Field field = object.getClass().getDeclaredField(fieldName);
//            //设置对象的访问权限，保证对private的属性的访问
//            field.setAccessible(true);
//            ApiModelProperty property = field.getAnnotation(ApiModelProperty.class);
//            if (null == property) {
//                return "";
//            }
//
//            return property.value();
//        } catch (Exception e) {
//            return "";
//        }
//
//    }
}
